package com.kakarote.examine.service.impl;

import com.kakarote.core.servlet.BaseServiceImpl;
import com.kakarote.examine.constant.ExamineLogHandlerType;
import com.kakarote.examine.constant.ExamineStatusEnum;
import com.kakarote.examine.constant.ExamineTypeEnum;
import com.kakarote.examine.constant.PassFlagEnum;
import com.kakarote.examine.entity.PO.ExamineDelegate;
import com.kakarote.examine.entity.PO.ExamineFlowFinal;
import com.kakarote.examine.entity.PO.ExamineRecordLog;
import com.kakarote.examine.mapper.ExamineDelegateMapper;
import com.kakarote.examine.service.IExamineDelegateService;
import com.kakarote.examine.service.IExamineFlowFinalService;
import com.kakarote.examine.service.IExamineRecordLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @author: admin
 * @version: v1.0
 * @date:2023/5/6
 */
@Service
public class ExamineDelegateServiceImpl extends BaseServiceImpl<ExamineDelegateMapper, ExamineDelegate> implements IExamineDelegateService {
    @Autowired
    private IExamineRecordLogService examineRecordLogService;

    @Autowired
    private IExamineFlowFinalService examineFlowFinalService;

    @Override
    public void updateDelegateStatus() {
        baseMapper.updateDelegateStatus();
    }

    /**
     * 处理流程委托
     *
     * @param flowId
     * @param recordId
     * @param label
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void disposeDelegate(Long flowId, Long recordId, Integer label) {
        ExamineFlowFinal flowFinal = examineFlowFinalService.lambdaQuery()
                .eq(ExamineFlowFinal::getFlowId, flowId)
                .eq(ExamineFlowFinal::getRecordId, recordId)
                .one();

        //排除抄送和条件审批
        List<Integer> exclude = new ArrayList<Integer>() {{
            add(ExamineTypeEnum.COPY.getType());
            add(ExamineTypeEnum.CONDITION.getType());
        }};
        if (flowFinal == null || exclude.contains(flowFinal.getExamineType())) {
            return;
        }

        //查询当前模块开启并且在时间段内的委托人
        List<ExamineDelegate> delegateList = getBaseMapper().queryDelegateByLabel(label);

        if (delegateList.size() == 0) {
            return;
        }
        List<ExamineRecordLog> recordLogs = examineRecordLogService.lambdaQuery()
                .eq(ExamineRecordLog::getRecordId, recordId)
                .eq(ExamineRecordLog::getFlowId, flowId)
                .list();
        for (ExamineRecordLog recordLog : recordLogs) {
            Long examineUserId = recordLog.getExamineUserId();
            if (examineUserId == null) {
                continue;
            }
            Optional<ExamineDelegate> first = delegateList.stream().filter(delegate -> delegate.getDelegateUserId().equals(examineUserId)).findFirst();

            if (!first.isPresent()) {
                continue;
            }
            examineRecordLogService.lambdaUpdate()
                    .set(ExamineRecordLog::getExamineStatus, ExamineStatusEnum.DELEGATE.getStatus())
                    .set(ExamineRecordLog::getPassFlag, PassFlagEnum.FORWARD_DELEGATE.getType())
                    .eq(ExamineRecordLog::getLogId, recordLog.getLogId()).update();

            recordLog.setRelTarget(recordLog.getLogId());
            recordLog.setLogId(null);
            recordLog.setExamineUserId(first.get().getTrusteeUserId());
            recordLog.setCreateTime(LocalDateTime.now());
            recordLog.setType(recordLog.getType());
            recordLog.setPassFlag(PassFlagEnum.FORWARD_DELEGATE.getType());
            recordLog.setHandlerType(ExamineLogHandlerType.DELEGATE.getType());
            examineRecordLogService.save(recordLog);
        }
    }

    @Override
    public Integer selectCountByDate() {
        return getBaseMapper().selectCountByDate();
    }


}
